﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_ProductStore_ProductStoreDetail : System.Web.UI.Page
{
    private int pTotal = 0, pInput = 0, pOutput = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["sDate"] != null && Request["eDate"] != null) //Truong hop don dat hang theo ngay
            {
                int mysDay, mysMonth, mysYear;
                int myeDay, myeMonth, myeYear;
                mysDay = int.Parse(Request["sDate"].ToString().Substring(6, 2));
                mysMonth = int.Parse(Request["sDate"].ToString().Substring(4, 2));
                mysYear = int.Parse(Request["sDate"].ToString().Substring(0, 4));

                myeDay = int.Parse(Request["eDate"].ToString().Substring(6, 2));
                myeMonth = int.Parse(Request["eDate"].ToString().Substring(4, 2));
                myeYear = int.Parse(Request["eDate"].ToString().Substring(0, 4));
                Picker1.SelectedDate = new DateTime(mysYear, mysMonth, mysDay);
                Picker2.SelectedDate = new DateTime(myeYear, myeMonth, myeDay);
            }
            BindStoreList();
            BindData();
        }
    }

    private void BindStoreList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlStore.DataSource = cls.Get_StoreByKindNoNoneProc().Tables[0];
        ddlStore.DataTextField = "StName";
        ddlStore.DataValueField = "StID";
        ddlStore.DataBind();
        item.Text = "Tất cả";
        item.Value = "0";
        ddlStore.Items.Insert(0, item);
        ddlStore.SelectedItem.Selected = false;

        if (Request.QueryString["StID"] != "" && Request.QueryString["StID"] != null)
            ddlStore.Items.FindByValue(Request.QueryString["StID"]).Selected = true;
    }


    private void BindData()
    {
        if (Request.QueryString["PDID"] != "" && Request.QueryString["PDID"] != null)
        {

            PccMsg msg = new PccMsg();
            string sDate = "", eDate = "";
            if (Picker1.SelectedDate.ToString("yyyyMMdd") != "00010101")
                sDate = Picker1.SelectedDate.ToString("yyyyMMdd");
            if (Picker2.SelectedDate.ToString("yyyyMMdd") != "00010101")
                eDate = Picker2.SelectedDate.ToString("yyyyMMdd");


            msg.CreateFirstNode("PdID", Request.QueryString["PDID"]);
            msg.CreateFirstNode("StartDate", sDate);
            msg.CreateFirstNode("EndDate", eDate);
            msg.CreateFirstNode("StId", ddlStore.SelectedValue);

            clsWebShop cls = new clsWebShop();
            DataSet ds = cls.Get_Store_ProductByCondition(msg.GetXmlStr);            
            DataTable dtSource = ds.Tables[0]; 
            grv_Master.DataSource = ds.Tables[0];
            grv_Master.DataBind();


        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblInputNext = (Label)e.Row.FindControl("lblInput");
            Label lblOutputNext = (Label)e.Row.FindControl("lblOutput");
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
            clsCommon clsCom=new clsCommon();
            lblPrice.Text = clsCom.FormatMoney(lblPrice.Text);

            int vTotal = 0;
            if (lblInputNext.Text != null && lblInputNext.Text != "")
            {
                vTotal += int.Parse(lblInputNext.Text);//Tong so luong nhap vao
                pInput += vTotal;
            }
            if (lblOutputNext.Text != null && lblOutputNext.Text != "")
            {
                pOutput += int.Parse(lblOutputNext.Text); //Tong so luong xuat ra
                vTotal -= int.Parse(lblOutputNext.Text);
                
            }
            pTotal += vTotal;
            lblTotal.Text = vTotal.ToString();

            e.Row.Cells[1].Text = "<a  onclick=\"window.open(this.href,'viewwin1','menu=0,scrollbars=1,status=1,location=0,resizable=1')\"  href='ProductStoreDetailInDay.aspx?StID=" + ddlStore.SelectedValue + "&PDID=" + Request.QueryString["PDID"] + "&StartDate=" + e.Row.Cells[1].Text + "'  target='viewwin1'>" + FormatDate(e.Row.Cells[1].Text) + "</a>";  

        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {

            e.Row.Cells[0].Text = "<a  onclick=\"window.open(this.href,'viewwin1','menu=0,scrollbars=1,status=1,location=0,resizable=1')\"  href='ProductStoreDetailInDay.aspx?StID=" + ddlStore.SelectedValue + "&PDID=" + Request.QueryString["PDID"] +"'  target='viewwin1'>" + "Tổng số lượng trong kho: " + ddlStore.SelectedItem.Text + "</a>"; 

            e.Row.Cells[0].ForeColor = System.Drawing.Color.FromName("Green");
            e.Row.Cells[0].ColumnSpan = 3;
            e.Row.Cells[3].Text = pInput.ToString();
            e.Row.Cells[3].ForeColor = System.Drawing.Color.FromName("Blue");
            e.Row.Cells[4].Text = pOutput.ToString();
            e.Row.Cells[4].ForeColor = System.Drawing.Color.FromName("Red");
            e.Row.Cells[5].Text = pTotal.ToString();
            e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("Navy");
            e.Row.Cells.RemoveAt(1);
            e.Row.Cells.RemoveAt(1);

        }

        /*if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Text = "<a href='ProductStoreDetailInDay.aspx?PDID=" + Request.QueryString["PDID"] + "&CodeID=" + Request.QueryString["CodeID"] + "&StoreDate=" + e.Row.Cells[1].Text + "' target='_blank'>" + FormatDate(e.Row.Cells[1].Text) + "</a>";

            int PreTotal = 0;
            try
            {
                PreTotal = int.Parse(grv_Master.Rows[e.Row.RowIndex - 1].Cells[4].Text);
            }
            catch
            { }

            if (e.Row.Cells[2].Text != "")
                PreTotal = PreTotal + int.Parse(e.Row.Cells[2].Text);
            if (e.Row.Cells[3].Text != "")
                PreTotal = PreTotal - int.Parse(e.Row.Cells[3].Text);
            e.Row.Cells[4].Text = PreTotal.ToString();


        }*/
    }

    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    protected void ddlStore_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}
